﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Drawing;
namespace DAO
{
    public class DAO_Employees
    {
        public static List<DTO_Employees> Select_All_Employees()
        {
            List<DTO_Employees> listEmployees = new List<DTO_Employees>();
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DAO_Connect.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = DAO_Listproc.select_all_employ;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_Employees Employees = new DTO_Employees();
                    Employees.Manv = dt.Rows[i]["Manv"].ToString();
                    Employees.Hoten = dt.Rows[i]["Hoten"].ToString();
                    if ((bool)dt.Rows[i]["Gioitinh"] == false)
                    {
                        Employees.GioiTinh = false;
                        Employees.StrGioiTinh = "Nữ";
                    }
                    else
                    {
                        Employees.GioiTinh = true;
                        Employees.StrGioiTinh = "Nam";
                    }
                    Employees.NgaySinh = DateTime.Parse(dt.Rows[i]["Ngaysinh"].ToString());
                    Employees.CMND = dt.Rows[i]["Cmnd"].ToString();
                    Employees.DiaChi = dt.Rows[i]["Diachi"].ToString();
                    Employees.Email = dt.Rows[i]["Email"].ToString();
                    Employees.Phone = dt.Rows[i]["Phone"].ToString();
                    Byte[] byt = (Byte[])dt.Rows[i]["Hinhanh"];
                    Employees.HinhAnh = cConver.ByteToImage(byt);
                    if ((bool)dt.Rows[i]["Nghiviec"] == false)
                        Employees.NghiViec = false;
                    else
                        Employees.NghiViec = true;
                    if ((bool)dt.Rows[i]["Trangthai"] == false)
                        Employees.TrangThai = false;
                    else
                        Employees.TrangThai = true;
                    listEmployees.Add(Employees);
                }
                DAO_Connect.Cmb().Close();
                return listEmployees;
            }
            catch (Exception ex)
            {
                return listEmployees;
            }
        }
        public static DataTable Select_All_Employees2()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DAO_Connect.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = DAO_Listproc.select_all_employ;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                return dt;
            }
        }
        public static bool SaveImage(byte[] image,string manv)
        {
            try
            {
                SqlConnection cnn = DAO_Connect.Cmb();
                SqlCommand cmm = new SqlCommand("proc_insert_Image", cnn);
                cmm.CommandType = CommandType.StoredProcedure;

                cmm.Parameters.Add("@hinhanh", SqlDbType.Image).Value = image;
                cmm.Parameters.Add("@Manv", SqlDbType.NVarChar,10).Value = manv;
                //cmm.Parameters.AddWithValue("@maloai", maloai);
                //cmm.Parameters.AddWithValue("@image", imageNV);
                if (cnn.State == ConnectionState.Closed)
                    cnn.Open();
                cmm.ExecuteNonQuery();
                cnn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
